#include "diagramctrl.h"
#include "dm/dmdef.h"

DiagramCtrl::DiagramCtrl(QObject *parent)
    : QObject{parent}
{
    m_param = {
        {false, true, true, true, QRectF(0, 0, 25, 25)},
        {false, true, false, true, QRectF(0, 0, 10, 10)},
        {false, true, false, true, QRectF(0, 0, 10, 10)},
        {false, false, false, true, QRectF(0, 0, 400, 75)},
        "",
        "",
        EQ_BW_DEFAULT,
        EQ_FREQUENCY_DEFAULT,
        EQ_GAIN_DEFAULT
    };
}

void DiagramCtrl::move(const QPointF &p)
{
    auto &center = m_param.center;
    auto &left = m_param.left;
    auto &right = m_param.right;
    center.rect.moveCenter(QPointF(center.henbale ? p.x() : center.rect.center().x(), center.venbale ? p.y() : center.rect.center().y()));

    left.rect.moveCenter(QPointF(p.x() - EQ_BW_DEFAULT - m_param.bw, p.y()));
    right.rect.moveCenter(QPointF(p.x() + EQ_BW_DEFAULT + m_param.bw, p.y()));
}

void DiagramCtrl::setBw(const int &bw)
{
    m_param.bw = bw;
}

void DiagramCtrl::setFreqGain(const double &freq, const double &gain)
{
    m_param.freq = freq;
    m_param.gain = gain;
}


DCtrlPushButton::DCtrlPushButton(const QString &text, bool hideLR, QWidget *parent)
    : QWidget{parent}
{
    center = {false, new QPushButton(text, this)};
    left = {false, new QPushButton(this)};
    right = {false, new QPushButton(this)};

    if (hideLR) {
        left.button->hide();
        right.button->hide();
    }

    connect(center.button, &QPushButton::pressed, this, [this](){this->center.dragging = true;});
    connect(center.button, &QPushButton::released, this, [this](){this->center.dragging = false;});
    connect(left.button, &QPushButton::pressed, this, [this](){this->left.dragging = true;});
    connect(left.button, &QPushButton::released, this, [this](){this->left.dragging = false;});
    connect(right.button, &QPushButton::pressed, this, [this](){this->right.dragging = true;});
    connect(right.button, &QPushButton::released, this, [this](){this->right.dragging = false;});

    center.button->setFocusPolicy(Qt::NoFocus);
    left.button->setFocusPolicy(Qt::NoFocus);
    right.button->setFocusPolicy(Qt::NoFocus);

    center.button->installEventFilter(this);
    left.button->installEventFilter(this);
    right.button->installEventFilter(this);
}

void DCtrlPushButton::moveCenter(const QPoint &p)
{
    center.button->move(p);
    left.button->move(left.button->x(), p.y());
    right.button->move(right.button->x(), p.y());
}

void DCtrlPushButton::moveLR(const unsigned int &offsetx)
{
    left.button->move(left.button->x() - offsetx, left.button->y());
    right.button->move(right.button->x() + offsetx, right.button->y());
}

bool DCtrlPushButton::eventFilter(QObject *watched, QEvent *event)
{
    // 判断事件源是否是我们的按钮，并且事件类型是鼠标按下
    if (watched == center.button && event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        qDebug() << "按钮被点击了，位置：" << mouseEvent->pos();

        // 如果希望按钮本身不再处理这个事件，返回true
        // 如果希望按钮继续处理这个事件（比如触发clicked信号），返回false
        return false; // 这里允许按钮继续处理事件
    }

    // 其他事件交给基类处理
    return QWidget::eventFilter(watched, event);
}
